package com.manager.system.controller;

import com.manager.common.vo.ResultVO;
import com.manager.common.utils.DataTools;
import com.manager.common.utils.LargeFileHttpUploadUtils;
import com.manager.model.Dictionary_Data;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/system")
public class SystemController {

    @RequestMapping("/getDicDataByType")
    public ResultVO getDicDataByType(@RequestBody  @RequestParam("dicType") String dicType){
        List<Dictionary_Data> list = DataTools.getDicDataByGroupId(dicType);
        for(int i = 0;i < 2;i++){
            Dictionary_Data dictionaryData = new Dictionary_Data();
            dictionaryData.setGroupId(dicType);
            dictionaryData.setGroupName(dicType);
            dictionaryData.setVal(String.valueOf(i));
            dictionaryData.setText("测试" + i);
            list.add(dictionaryData);
        }
        return ResultVO.success(list);
    }

    @PostMapping("/uploadFile")
    public ResultVO uploadFile(@RequestParam("data") MultipartFile file , HttpServletRequest request,
                           HttpServletResponse response){
        response.setContentType("application/json");
        response.setHeader("Access-Control-Allow-Origin", "*");
       return LargeFileHttpUploadUtils.uploadFile(request,file);
    }

    @PostMapping("/mergeFile")
    public ResultVO merge(HttpServletRequest request,
                               HttpServletResponse response){
        response.setContentType("application/json");
        response.setHeader("Access-Control-Allow-Origin", "*");
       try {
           return LargeFileHttpUploadUtils.meragePartFile(request);
       }catch (Exception e){
           e.printStackTrace();
           return ResultVO.failed(e.getMessage());
       }
    }
}
